package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.constant.basic.PlatformRuleTypeEnum;
import com.ssy.lingxi.common.constant.order.OrderModeEnum;
import com.ssy.lingxi.common.constant.order.OrderTypeEnum;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.paas.MemberConfigControllerFeign;
import com.ssy.lingxi.member.merchant.api.model.vo.paas.MemberRuleFeignVO;
import com.ssy.lingxi.order.api.model.vo.paas.OrderRuleFeignVO;
import com.ssy.lingxi.order.api.paas.OrderConfigFeignService;
import com.ssy.lingxi.system.dao.RuleConfigurationRepository;
import com.ssy.lingxi.system.dto.request.RuleConfigurationRequest;
import com.ssy.lingxi.system.dto.response.RuleConfigurationListResponse;
import com.ssy.lingxi.system.entity.RuleConfiguration;
import com.ssy.lingxi.system.model.response.RuleVO;
import com.ssy.lingxi.system.service.RuleConfigurationService;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 平台规则配置
* @author wrc
* @date 2020/7/13
*/
@Service
public class RuleConfigurationServiceImpl  implements RuleConfigurationService {
    @Resource
    private RuleConfigurationRepository ruleConfigurationRepository;

    @Resource
    private OrderConfigFeignService orderConfigFeignService;

    @Resource
    private MemberConfigControllerFeign memberConfigControllerFeign;

    /**
     * 查询平台规则配置
     * @return 查询结果
     */
    @Override
    public Wrapper<List<RuleConfigurationListResponse>> ruleConfigurationList() {
        List<RuleConfigurationListResponse> list=new ArrayList<>();
        List<RuleConfiguration> all = ruleConfigurationRepository.findAll(Sort.by("id").ascending());
        Map<Integer, List<RuleConfiguration>> collect = all.stream().collect(Collectors.groupingBy(RuleConfiguration::getPlatformType));
        collect.keySet().forEach(type->{
            RuleConfigurationListResponse ruleConfigurationListResponse=new RuleConfigurationListResponse();
            ruleConfigurationListResponse.setPlatformType(type);
            ruleConfigurationListResponse.setPlatformTypeName(collect.get(type).get(0).getPlatformTypeName());
            ruleConfigurationListResponse.setRuleConfigurations(null==collect.get(type)?new ArrayList<>():collect.get(type));
            list.add(ruleConfigurationListResponse);
        });
        return Wrapper.success(list);
    }

    /**
     * 修改平台配置
     * @param request 接口参数
     * @return 修改结果
     */
    @Transactional
    @Override
    public Wrapper<Void> ruleConfigurationUpdate(RuleConfigurationRequest request) {
        //同步至会员服务
        List<MemberRuleFeignVO> memberRules = request.getRuleConfigurations().stream().map(rule -> {
            MemberRuleFeignVO ruleVO = new MemberRuleFeignVO();
            ruleVO.setRuleType(rule.getPlatformType());
            ruleVO.setMethodCode(rule.getCode());
            ruleVO.setMethodName(rule.getPlatformWayName());
            ruleVO.setStatus(rule.getIsPitchOn());
            return ruleVO;
        }).collect(Collectors.toList());

        Wrapper<Void> memberResult = memberConfigControllerFeign.updateOrInsertMemberRules(memberRules);
        if(memberResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(memberResult.getCode(), memberResult.getMessage());
        }

        //同步至订单服务
        List<OrderRuleFeignVO> orderRules = request.getRuleConfigurations().stream().map(rule -> {
            OrderRuleFeignVO ruleVO = new OrderRuleFeignVO();
            ruleVO.setRuleType(rule.getPlatformType());
            ruleVO.setMethodCode(rule.getCode());
            ruleVO.setMethodName(rule.getPlatformWayName());
            ruleVO.setStatus(rule.getIsPitchOn());
            return ruleVO;
        }).collect(Collectors.toList());

        Wrapper<Void> orderResult = orderConfigFeignService.updateOrInsertOrderRules(orderRules);
        if(orderResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(orderResult.getCode(), orderResult.getMessage());
        }

        ruleConfigurationRepository.saveAll(request.getRuleConfigurations());
        return Wrapper.success();
    }

    /**
     * 售后订单类型查询
     */
    @Override
    public Wrapper<List<RuleVO>> getOrderType() {
        List<RuleConfiguration> ruleConfigurations = ruleConfigurationRepository.findAllByPlatformTypeAndIsPitchOn(PlatformRuleTypeEnum.ORDER_TYPE.getCode(), EnableDisableStatus.ENABLE.getCode(), Sort.by("code").descending());
        return Wrapper.success(ruleConfigurations.stream().map(temp -> {
            RuleVO ruleVO = new RuleVO();
            ruleVO.setIsPitchOn(temp.getIsPitchOn());
            ruleVO.setPlatformType(temp.getPlatformType());
            ruleVO.setPlatformTypeName(PlatformRuleTypeEnum.ORDER_TYPE.getName());
            ruleVO.setPlatformWayName(OrderTypeEnum.getNameByCode(temp.getCode()));
            ruleVO.setId(temp.getCode());
            return ruleVO;
        }).collect(Collectors.toList()));
    }

    /**
     * 查询下单模式
     *
     * @return 查询结果
     */
    @Override
    public Wrapper<List<RuleVO>> getOrderMode() {
        List<RuleConfiguration> ruleConfigurations = ruleConfigurationRepository.findAllByPlatformTypeAndIsPitchOn(PlatformRuleTypeEnum.ORDER_MODE.getCode(), EnableDisableStatus.ENABLE.getCode(), Sort.by("code").descending());
        return Wrapper.success(ruleConfigurations.stream().map(temp -> {
            RuleVO ruleVO = new RuleVO();
            ruleVO.setIsPitchOn(temp.getIsPitchOn());
            ruleVO.setPlatformType(temp.getPlatformType());
            ruleVO.setPlatformTypeName(PlatformRuleTypeEnum.ORDER_MODE.getName());
            ruleVO.setPlatformWayName(OrderModeEnum.getNameByCode(temp.getCode()));
            ruleVO.setId(temp.getCode());
            return ruleVO;
        }).collect(Collectors.toList()));
    }
}
